<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Document</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/aui_font.css">
    <link rel="stylesheet" href="../../css/news.css">
    <link rel="stylesheet" href="../../css/frame2.css">
    <link rel="stylesheet" href="../../css/swiper.css">
    <style>
        .msglist-wrap .u-wrap .list {
            border-bottom: 1px solid #fafafa;
            padding: 5px 0;
            margin: 0 10px;
        }

        .msglist-wrap .u-wrap .tx {
            overflow: visible;
        }

        .msglist-wrap .u-wrap .tx img {
            border-radius: 50%;
            width: 50px;
            height: 50px;
        }

        .msglist-wrap .u-wrap .list .name {
            font-size: 14px;
        }

        .msglist-wrap .u-wrap .msg {
            color: #999999;
        }

        .msglist-wrap .u-wrap .time {
            color: #B3B3B3;
            font-size: 10px;
        }

        /* 消息提醒数 */
        .msglist-wrap .u-wrap .list .yuan {
            position: absolute;
            right: -8px;
            top: -4px;
        }

        /* 官方 */
        .sys-tag {
            background: #7ED0AE;
            color: #fff;
            border-radius: 15px;
            font-size: 10px;
            padding: 0 5px;
        }

        .msglist-wrap .u-wrap .tx img.jiazu-tag {
            width: 60px;
            position: absolute;
            height: auto;
            bottom: 0;
        }

        /* 轮播 */
        .swiper-container {
            margin: 7px 14px;
        }

        .swiper-container img {
            width: 100%;
            border-radius: 7px;
            height: auto;
        }

        /* 置顶 */
        .msglist-wrap .u-wrap .top,
        .msglist-wrap .u-wrap .top .list {
            background: #f3f3f3;
        }

        .msglist-wrap .list-wrap {
            width: 100vw;
            overflow: hidden;
            box-sizing: border-box;
        }

        .msglist-wrap .u-wrap .list {
            width: 100vw;
            box-sizing: border-box;
            -webkit-transform: translateX(0px);
            transition: -webkit-transform 500ms ease-in-out;
        }

        .msglist-wrap .u-wrap .slid-left .list {
            -webkit-transform: translateX(-100px);
        }

        .slid-left .top-btn {
            -webkit-transform: translateX(calc(100vw - 100px));
        }

        .slid-left .del-btn {
            -webkit-transform: translateX(calc(100vw - 100px));
        }

        .msg-btn {
            position: absolute;
            color: #fff;
            text-align: center;

            -webkit-transform: translateX(100vw);
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: -webkit-transform 500ms ease-in-out;
        }

        .del-btn {
            width: 100px;
            background: red;
        }
        .name-wrap{
            display: flex;
            align-items: center;
        }
        .cle-btn{
            float: right;border: 1px solid #999999;border-radius: 8px;padding: 3px 5px;font-size: 14px;
        }
    </style>
</head>

<body>
    <div class="msglist-wrap" id="view" v-cloak>
        <div class="swiper-container" v-if="lbList.length>0 && !ios">
            <div class="swiper-wrapper">
                <div class="swiper-slide" v-for="(m, index) in lbList"><img :src="returnImg(m.img)" onerror="this.src='../../image/error-img.png'" alt="" @click="openSlid(index)"></div>
            </div>
        </div>
        <div class="swiper-container" v-if="ffList.length>0 && !ios">
            <span class="cle-btn" v-if="view.userInfo.sex == '女'"  onclick="clearmsg();">清理搭讪</span>
            <span class="cle-btn" style="margin-right: 10px;" v-if="batch_btn == 0" onclick="batch_msg();">批量删除聊天</span>
            <span class="cle-btn" style="margin-right: 10px;" v-if="batch_btn == 1 " @click="batch_del();">确定删除</span>
            <span class="cle-btn" style="margin-right: 10px;" v-if="batch_btn == 1 " onclick="cancel_clearmsg();">取消</span>
        </div>
        <ul class="u-wrap" v-if="ffList.length">
            <!-- <li class="list-wrap" v-for="(m, index) in ffList2" v-if="m.id==2 || m.id==3" @click="openChat(m)">
                <div class="list">
                    <div class="tx img-h">
                        <img :src="returnImg(m.pic)" onerror="this.src='../../image/touxiang.png'" alt="">
                    </div>
                    <div class="content">
                        <div class="flex-bt">
                            <div class="name-wrap" style="position: relative;">
                                <span class="name">{{m.name}}</span>
                            </div>
                        </div>
                        <div class="flex-bt new-margin-t-10">
                            <p class="msg aui-ellipsis-1">{{m.content}}</p>
                        </div>
                    </div>
                </div>
            </li> -->
            <li class="list-wrap" :class="{'top':m.top==1,'slid-left':m.s == 1}" v-for="(m, index) in ffList" :id="m.userid" :key="index">
                <div class="list" @click="goTalk(index)"   @touchstart="touchstartF($event, index);" @touchmove="touchmoveF($event, index);"  >
               
                    <div class="tx img-h">
                        <img :src="returnImg(m.sumb_headimg)" onerror="this.src='../../image/touxiang.png'" alt="">
                        <img :src="returnImg(m.noble_circle)" v-if="m.noble_circle && !ios" class="tx_cover" alt="">
                        <span class="yuan" v-if="m.count > 0">{{m.count>99? '99+' : m.count}}</span>
                        <img v-if="m.is_cluster==1" class="jiazu-tag" src="../../image/icon/ms_family_top_icon.png" alt="">
                    </div>
                    <div class="content">
                        <div class="flex-bt">
                            <div class="name-wrap" style="position: relative;">
                                <span class="name"  style="font-size:16px ;">{{m.name}}
                                    <span class="sys-tag" v-if="m.kefu == 1">{{_i18n('官方')}}</span>

                                </span>
                                <span class="new-tag man-ntag" :class="{'woman-ntag':m.sex=='女'}" v-if="m.realname_auth==2 && !ios"></span>
                                <span class="new-tag rz-tag" v-if="m.is_identity_authentication==2 && !ios"></span>
                                <img v-if="m.crown_icon" class="new-noble" :src="returnImg(m.crown_icon)" onerror="this.src='../image/error-img.png'" alt="">
                                <img v-if="m.noble_icon" class="new-noble" :src="returnImg(m.noble_icon)" onerror="this.src='../image/error-img.png'" alt="">
                                <img v-if="m.is_topup" class="new-noble" :src="returnImg(m.is_topup)" onerror="this.src='../image/error-img.png'" alt="">
                            </div>
                            <span class="time" v-if="m.city"><i class="aui-iconfont aui-icon-location"></i>{{m.city}}</span>
                        </div>
                        <div class="flex-bt new-margin-t-10" >
                            <p class="msg aui-ellipsis-1" v-if="m.styles == 2" v-html="m.content"  style="max-width: 55%;color: red;position: relative;top: -15px;"></p>
                            <p class="msg aui-ellipsis-1" v-else style="max-width: 55%;font-size: 14px;">{{clearHtml(m.content)}}</p>

                            <span class="time" v-if="batch_btn == 0">{{m.addtime}}</span>
                            <span v-else><input type="checkbox" @click="xuanze(index);" style="width: 25px;height: 25px;" ></span>
                        </div>
                        
                    </div>
                   
                </div>

                

                <div class="del-btn msg-btn" @click.stop="delMsg(index)">{{_i18n('删除')}}</div>
            </li>
        </ul>
        <div id="no_more" :class="{'null': ffList.length == 0}"></div>
    </div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/jquery.js"></script>
<script src="../../script/ffkj.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/swiper.js"></script>
<script src="../../script/limitchat.js"></script>
<script src="../../json/liao1.js"></script>
<script src="../../script/tent/im.js"></script>
<script src="../../script/tent/tencentCloudChat.js"></script>
<script>
    var view = new Vue({
        el: '#view',
        data: {
            imgurl: imgurl,
            // ffList: ret.data.result.result, // 私聊列表
            ffList: [], // 私聊列表
            ffList2: [], // 系统消息
            ffInfo: {},
            userInfo:{},
            isVip: 0,
            sysInfo: [], // 系统通知 所有人的
            kefu: 0, // 客服id
            ios: 1,
            lbList: [],
            x: 0,
            y: 0,
            swipeX: false,
            batch_btn: 0,
            swipeY: false,
            lastNextSeq :0
        },
        methods: {
            delMsg(index) {
                var touserid = view.ffList[index].userid;

                // console.log('删除333',JSON.stringify(view.ffList[index]))
                
				deleteConversation(view.ffList[index].conversationID,function(){
					view.ffList.splice(index, 1);
					_ajax('home/Privatechat/delChat', function(res, err) {}, {
					    uid: $api.getStorage('userid'),
					    touid: touserid
					})
				})
            },
            touchstartF: function(event, index) {
                if (view.ffList[index].kefu == 1) return;
                view.x = event.changedTouches[0].pageX;
                view.y = event.changedTouches[0].pageY;

                view.swipeX = true;
                view.swipeY = true;
            },
            touchmoveF: function(event, index) {
                if (view.ffList[index].kefu == 1) return;
                var e = event.currentTarget;
                var X = event.changedTouches[0].pageX;
                var Y = event.changedTouches[0].pageY;

                var swipeX = view.swipeX;
                var swipeY = view.swipeY;
                var x = view.x;
                var y = view.y;
                
                if(view.ffList[index].userID){
                    var uid = view.ffList[index].userID
                }else{
                    var uid = view.ffList[index].userid;
                }
                console.log(JSON.stringify(view.ffList[index]))
                // 左右滑动
                if (swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0) {
                    // 阻止事件冒泡
                    event.stopPropagation();
                    if (X - x > 10) {
                        event.preventDefault();
                        // view.ffList[index].s = 0;
						document.getElementById(uid).classList.remove('slid-left')
                    }
                    if (x - X > 10) {
                        event.preventDefault();
                        // view.ffList[index].s = 1;
						document.getElementById(uid).classList.add('slid-left')
                    }
                    view.swipeY = false;
                }
                // 上下滑动
                if (swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                    view.swipeX = false;
                }
            },
            // 轮播跳转
            openSlid(index) {
                console.log(index);
                var _this = this;
                var data = _this.lbList[index];
                if (!data.url) {
                    
                } else {
                    _url({
                        url: data.url,
                        title: '详情'
                    }, 'web')
                }
            },
            // 系统消息跳转
            openChat(result) {
                var id = result.id;
                var data = result.data;
                if (data && typeof data == 'string') {
                    data = JSON.parse(data);
                }
                switch (id) {
                    case 1:
                        _url({
                            url: 'frame3/visitor',
                            title: result.name,
                            num: data.count
                        })
                        break;
                    case 2:
                        _index(5)
                        break;
                    case 3:
                        _url({
                            url: 'frame0/groupchat',
                            title: result.name
                        })
                        break;
                    default:
                        break;
                }
            },
            // 打开聊天窗口
            goTalk(index) {
                var _this = this;
                if(this.batch_btn == 0){
                    var data = _this.ffList[index];
                    console.log( JSON.stringify( data ))
                    if(data.userid){
                        var uid = data.userid;
                    }else{
                        var uid = data.userID
                    }
                    
                    var name = data.name;
                    if (data.is_cluster == 1) {
                        // 群聊
                        openQun(data.cluster_id, name, data.cluster_types)
                        return;
                    }
                    setMessageRead(uid,function(){
                        _url({
                            touserid: uid,
                            title: '与' + name + '的聊天',
                            moreType: 1,
                            conversationID:data.conversationID,
                            isPinned:data.isPinned
                        }, 'frame0/liaotian_win')
                    })
                }
                
            },
            _url(param, url) {
                _url(param, url);
            },
            // 返回图片路径
            returnImg(url) {
                return returnImg(url);
            },

            // 清除html
            clearHtml(s) {
                return clearHtml(s);
            },
            xuanze(index){
                var b_x = view.ffList[index].b_x;
                if(b_x == 0){
                    view.ffList[index].b_x = 1;
                }else{
                    view.ffList[index].b_x = 0;
                }
                
            },
            batch_del(){
                _confirm({
                    msg: '确认批量删除聊天吗？，删除列表将不显示，聊天记录还存在'
                }, function(bIndex) {
                    if (bIndex == 1) {

                        var data = view.ffList ;
                        data.map(function(value,index){
                            
                            if(value.lastMessage){
                                var datass = JSON.parse(value.lastMessage.customElem.data)
                                console.log(JSON.stringify(value))
                                console.log('33333333333333',value.b_x)
                                console.log('b_x',value.b_x)
                                if(parseInt(value.b_x) == 1){
                                    //if(value)
                                    var touid = value.userid ? value.userid : datass.userid
                                    console.log('是否删除搭讪消息' + touid)
                                    console.log('是否删除搭讪消息value.userid' + value.userid)
                                    console.log('是否删除搭讪消息datass.userID' + datass.userid)
                                    deleteConversation('c2c_'+touid,function(){ 
                                        view.ffList.splice(index, 1);
                                    
                                    })
                                }
                            }
                            
                        })
                        view.batch_btn = 0
                        _msg('删除成功')
                        getNowMsgList(1);


                        // console.log('batch_del')
                        // console.log(JSON.stringify(view.ffList))
                        // var user_id_arr = '';
                        // for (var i = 0, len = view.ffList.length; i < len; i++) {
                        //     if(view.ffList[i].b_x == 1){
                        //         //user_id_arr.push(view.ffList[i].userid)
                        //         user_id_arr += view.ffList[i].userid + ',';
                        //     }
                        // }
                        // if(user_id_arr){
                        //     // _ajax('home/Privatechat/delChat', function(res, err) {
                        //     //     if (res && res.code == 200) {
                        //     //         _msg('删除成功')
                        //     //         view.batch_btn = 0
                        //     //         setTimeout(() => {
                        //     //             getNowMsgList(1);
                        //     //         }, 1000);
                        //     //         if (ret && ret.code == 200) {
                        //     //             pushMsg($api.getStorage('userid'))
                        //     //         }
                                
                        //     //     } else {
                        //     //         _msg('删除失败');
                        //     //     }
                        //     // }, {
                        //     //     uid: $api.getStorage('userid'),
                        //     //     touid: user_id_arr,
                        //     // })
                        // }
                        // console.log(user_id_arr);
                    }
                })
            }
        }
    })
    var sysTime = 1000;
    var sysNowType = 0;
    var feeObj = {};
    apiready = function() {
        initStorage()
        _listener('viewappear', function() {
            //location.reload();
           //getNowMsgList(1);
           api.refreshHeaderLoadDone();
        })

        _getUser(function(ret) {
           // console.log(JSON.stringify(ret))
            view.userInfo = ret.result;
        })

       // view.userInfo = api.pageParam['info'].self;
       

        console.log('xiaoxiwin_2222');
		getSlide(2, function(data) {
            view.lbList = data;
            setTimeout(function() {
                var mySwiper = new Swiper('.swiper-container', {
                    loop: true, // 循环模式选项
                })
            }, 300)
        })
        view.ios = $api.getStorage('shang');
        openService()

        _listener('viewappear', function(ret) {
            $api.rmStorage('dialogueUserid')
        })

       
       
        getNowMsgList(1);
        _shangla(function() {
            ConNextSeg = 0;
            getNowMsgList(1);
        }, 1)

        _scrollToBottom(function(){
            //ConNextSeg = 0;
            getNowMsgList(1);
            // console.log('下拉了111')
        })
        allclearmsg();

    }



    // 获取客服id
    function openService() {
        _ajax('api/Member/kefu', function(ret, err) {
            if (ret && ret.data && ret.data.id) {
                view.kefu = ret.data.id
            }
        })
    }

    // 获取系统通知
    function getAllSysInfo() {
        _ajax('Home/User/sysmsg', function(ret, err) {
            if (ret && ret.code == 200) {
                view.sysInfo = ret.result;

            }
        })
    }

    // 即时刷新的消息
    function getNowMsgList(type) {
		if(typeof(type) == 'undefined'){
			type = 0
		}
        if (!$api.getStorage('userid')) {
            // console.log('没有用户ID')
            // api.refreshHeaderLoadDone();
            // return;
        }
        /**
		 var obj = {
		     userid: $api.getStorage('userid'),
		     page_types:type
		 };
		 _ajax('Home/User/syslist', function(ret, err) {},{
		     user_id: $api.getStorage('userid')
		 })
		 * **/
		getConversations(function(){
			api.refreshHeaderLoadDone();
			//view.ffList = [];
		},function(res){
            
			api.refreshHeaderLoadDone();
			view.ffList = res;
           // 
            console.log(JSON.stringify( res ))
            var start = new Date(new Date().toLocaleDateString()).getTime();
			res.map(function(value,index){
                var uid = value.userID
                var sex =  $api.getStorage('sex');
               
              
				view.ffList[index].conversationID = value.conversationID
				view.ffList[index].s = 0
                view.ffList[index].b_x = 0
                
				view.ffList[index].count = value.unreadCount;
				view.ffList[index].name = value.showName
				if(value.faceUrl){
					view.ffList[index].sumb_headimg = value.faceUrl
				}
				if(value.lastMessage && value.lastMessage.customElem && value.lastMessage.customElem.data){
					if(value.lastMessage.status == 6){
						view.ffList[index].content = '[该消息已被撤回]';
					}else{
						view.ffList[index].content = getConversaFieldText(value.lastMessage.customElem.data);
					}
                    view.ffList[index].styles = 1;
                    var datass = JSON.parse(value.lastMessage.customElem.data)
                    console.log('类型',datass)
                    if(parseInt(datass.type) ==-3){
                        view.ffList[index].styles = 2;
                    }


					view.ffList[index].addtime = getdate(value.lastMessage.timestamp)
				}else{
					view.ffList[index].addtime = ''
				}
                var data = _getStorage('to_user_' + value.userID);
                if(data){
                    try{
                        for(var item in data){
                            if(!value.showName && item == 'name'){
                                view.ffList[index].name = data[item];
                            }else if(['content','addtime','count'].indexOf(item) == -1){
                                view.ffList[index][item] = data[item]
                            }  
                        }
                    }catch(e){
                        
                    }
                }else{
                    getUserInfos(value.userID,function(data){     


                        try{
                            for(var item in data){
                                if(!value.showName && item == 'name'){
                                    view.ffList[index].name = data[item];
                                }else if(['content','addtime','count'].indexOf(item) == -1){
                                    view.ffList[index][item] = data[item]
                                }  
                            }
                        }catch(e){
                            
                        }
                    })
                }

			});
            allclearmsg();
		},function(res){

			api.refreshHeaderLoadDone();
		})
    }

    
	function compare(protoTypeName) {
			return function(object1,object2){
				var val1 = object1[protoTypeName];
				var val2 = object2[protoTypeName];
				if(val1<val2){
					return 1;
				}else if(val1>val2){
					return 2;
				}else{
					return 0;
				}
			}
		}



    function batch_msg(){
        view.batch_btn = 1
    }



    function cancel_clearmsg(){
        view.batch_btn = 0
    }

    function allclearmsg(){
        var data = view.ffList ;
        var timestamp = Date.now(); 
        var start = parseInt((timestamp / 1000) - (60 * 60 * 2 ))

        console.log('推流消息222222222',start)
        console.log((start / 1000));
        data.map(function(value,index){
            console.log('444444444444',JSON.stringify( value ))
            if(value.lastMessage){
                var datass = JSON.parse(value.lastMessage.customElem.data)
                console.log('推流消息33333',JSON.stringify( datass ))
                console.log('推流消息33333444555', value.lastMessage.timestamp)
                if((parseInt(datass.type) ==-3 && value.lastMessage.timestamp <= start)){
                    //console.log('是否删除搭讪消息' + JSON.stringify( datass ))
                    deleteConversation('c2c_'+datass.userid,function(){ 
                        view.ffList.splice(index, 1);
                        
                    })
                }
            }
            
            
            //console.log(datass.type)
        })
        // getNowMsgList(1)
    }

    // 清空消息
    function clearmsg() {
        _confirm({
            msg: '是否删除搭讪消息??'
        }, function(bIndex) {
            if (bIndex == 1) {
                var data = view.ffList ;

                console.log('消息列表111343434',JSON.stringify( data ))
                data.map(function(value,index){
                    
                    if(value.lastMessage){
                        var datass = JSON.parse(value.lastMessage.customElem.data)

                        if(parseInt(datass.type) == -3){
                            console.log('是否删除搭讪消息' + JSON.stringify( datass ))
                            deleteConversation('c2c_'+datass.userid,function(){ 
                                view.ffList.splice(index, 1);
                            
                            })
                        }
                    }
                   
                    
                    // console.log(datass.type)
                })
                getNowMsgList(1)
                // _ajax('home/privatechat/allRead', function(ret, err) {
                //     _msg(ret.msg);
                //     if (ret && ret.code == 200) {
                //         api.setAppIconBadge({badge:0});
				// 		var msgNum = {cnum:0,mnum:0}
				// 		$api.setStorage('msgNum', msgNum);
				// 		_send('msgNum', msgNum);
				// 		// 未完成
				// 		markAllMessageAsRead()
				// 		getNowMsgList(1);
                //     }
                // }, {
                //     user_id: $api.getStorage('userid')
                // })
            }

        })
    }
	
	
	
	
</script>
</html>